import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/view/Layout/LayoutPage.vue'),
      children: [
        {
          path: '',
          component: () => import('@/view/Home/HomePage.vue'),
        },
        {
          path: 'category/:id',
          component: () => import('@/view/Category/CategoryPage.vue'),
        },
        {
          path: 'category/subcategory/:id',
          component: () => import('@/view/Subcategory/SubcategoryPage.vue'),
        },
        {
          path: 'detail/:id',
          component: () => import('@/view/Detail/DetailPage.vue'),
        },
        {
          path: 'cartList',
          component: () => import('@/view/Cartlist/CartListPage.vue'),
        },
        {
          path: 'checkout',
          component: () => import('@/view/Checkout/CheckoutPage.vue'),
        },
        {
          path: 'pay/:id',
          component: () => import('@/view/Pay/PayPage.vue'),
        },
        {
          path: 'member',
          component: () => import('@/view/Member/MemberPage.vue'),
          children: [
            {
              path: '',
              component: () => import('@/view/Member/components/MemberCenter.vue'),
            },
            {
              path: 'myOrder',
              component: () => import('@/view/Member/components/MyOrder.vue'),
            },
          ],
        },
      ],
    },
    {
      path: '/login',
      component: () => import('@/view/Login/LoginPage.vue'),
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    // 如果有保存的位置（浏览器前进/后退）
    if (savedPosition) {
      return savedPosition
    } else {
      // 总是滚动到顶部
      return { top: 0 }
    }
  },
})

export default router
